//
//  PoisonCardView.swift
//  Game
//
//  Created by woniu on 2025/9/25.
//

import UIKit

class PoisonCardView: UIView {
    
    // 卡片正面和背面
    private var frontView: UIView!
    private var backView: UIView!
    
    // 显示单词的标签
    private var wordLabel: UILabel!
    
    // 卡片是否已翻开
    var isFlipped: Bool = false
    
    // 卡片上的单词
    var word: String = "" {
        didSet {
            wordLabel.text = word
        }
    }
    
    // 是否为毒药卡片
    var isPoison: Bool = false {
        didSet {
            if isPoison && isFlipped {
                // 毒药卡片的特殊标识样式
                frontView.backgroundColor = UIColor(red: 0.8, green: 0.2, blue: 0.2, alpha: 1.0)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        // 创建卡片背面
        backView = UIView()
        backView.backgroundColor = Colors.getRandomWarmColor()
        backView.layer.cornerRadius = 8
        backView.layer.masksToBounds = true
        backView.layer.borderColor = UIColor.white.cgColor
        backView.layer.borderWidth = 1
        addSubview(backView)
        
        // 创建卡片正面
        frontView = UIView()
        frontView.backgroundColor = Colors.getRandomWarmColor()
        frontView.layer.cornerRadius = 8
        frontView.layer.masksToBounds = true
        frontView.layer.borderColor = UIColor.white.cgColor
        frontView.layer.borderWidth = 1
        frontView.isHidden = true
        addSubview(frontView)
        
        // 单词标签
        wordLabel = UILabel()
        wordLabel.textAlignment = .center
        wordLabel.font = UIFont.boldSystemFont(ofSize: 45)
        wordLabel.textColor = .white
        wordLabel.numberOfLines = 0
        frontView.addSubview(wordLabel)
        
        // 设置约束
        backView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        frontView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        wordLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(5)
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.bottom.equalToSuperview().offset(-5)
        }
    }
    
    // 翻转卡片
    func flipCard(completion: (() -> Void)? = nil) {
        guard !isFlipped else { return }
        
        isFlipped = true
        Utils.playTapSound()
        // 动画翻转卡片
        UIView.transition(from: backView, to: frontView, duration: 0.5, options: [.transitionFlipFromRight, .showHideTransitionViews]) { _ in
            // 如果是毒药卡片，改变样式
            if self.isPoison {
                self.frontView.backgroundColor = UIColor(red: 0.8, green: 0.2, blue: 0.2, alpha: 1.0)
            }
            completion?()
        }
    }
    
    // 重置卡片
    func resetCard() {
        isFlipped = false
        isPoison = false
        frontView.backgroundColor = Colors.getRandomWarmColor()
        frontView.isHidden = true
        backView.isHidden = false
        
        // 直接切换视图（无动画）
        backView.layer.removeAllAnimations()
        frontView.layer.removeAllAnimations()
    }
}
